package com.sixmac.controller;

import com.sixmac.annotation.AuthUser;
import com.sixmac.core.annotation.Validator;
import com.sixmac.core.base.model.Format;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.model.User;
import com.sixmac.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by admin on 2018/6/9.
 */
@Controller
@ResponseBody
public class CategoryController extends ApiController {
    @Autowired
    CategoryService categoryService;

    /**
     * @api {post} /api/category 我关注的分类
     * @apiName category.category
     * @apiGroup category
     * @apiParam {String}  token  token
     * @apiSuccess {String} user.id 分类id
     * @apiSuccess {String} user.name 分类名字
     * @apiSuccess {String} user.pid 分类的父名字 2，展览，3品牌，以此类推
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/category")
    public AjaxResult getCategory(User user) {
        return this.categoryService.getCategory(user.getId());
    }

    /**
     * @api {post} /api/getAllCategory 获取所有分类
     * @apiName category.getAllCategory
     * @apiGroup category
     * @apiSuccess {String} list.id 分类id
     * @apiSuccess {String} list.name 分类名字
     * @apiSuccess {String} list.pid 父分类的名字 2，展览，3品牌，以此类推 pid为0的表示父分类
     * @apiError {Integer} 1
     */
    @RequestMapping("/getAllCategory")
    public AjaxResult getAllCategory() {
        AjaxResult res = new AjaxResult();
        List map = Db.selectList("select * from t_category");
        return res.setData(CMap.init().put("list",map));
    }

    /**
     * @api {post} /api/editCategory （保存）编辑分类
     * @apiName category.editCategory
     * @apiGroup category
     * @apiParam {String}  token  token
     * @apiParam {String}  categoryIds  用户关注的分类ids 比如 1,2,3 多个用英文逗号分开
     * @apiSuccess {String} user.id 分类id
     * @apiSuccess {String} user.name 分类名字
     * @apiSuccess {String} user.pid 分类的父名字 2，展览，3品牌，以此类推
     * @apiError {Integer} 1
     */
    @AuthUser
    @RequestMapping("/editCategory")
    @Validator(name = {"categoryIds"}, format = {Format.NOT_NULL})
    public AjaxResult editCategory(User user, String categoryIds) {
        User u = Blade.create(User.class).findById(user.getId());
        u.setCategoryIds(categoryIds);
        Blade.create(User.class).update(u);
        return new AjaxResult().setData(CMap.init().put("user", u));
    }


}
